package com.ericsson.enap.modules.monitor.service.impl;

import com.ericsson.enap.modules.topology.entity.CabSwitch;
import com.ericsson.enap.modules.monitor.entity.SwitchInterfaceStatus;
import com.ericsson.enap.modules.monitor.entity.SwitchPortData;
import com.ericsson.enap.modules.monitor.entity.SwitchTotalData;
import com.ericsson.enap.modules.monitor.service.SwitchEquipDataService;
import com.ericsson.common.util.TelnetUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("extreme")
public class SwitchDataServiceEXTREMEImpl implements SwitchEquipDataService {
    @Override
    public String getAlarm(TelnetUtils telnetUtils, String command) {
        String result = telnetUtils.sendCommand(command);
        return result;
    }

    @Override
    public SwitchTotalData getUpTime(TelnetUtils telnetUtils, String command) {
        SwitchTotalData switchTotalData = new SwitchTotalData();
        String result = telnetUtils.sendCommand(command);
        String[] s = result.split("\n");
        String uptime=s[17].substring(s[17].indexOf(":")+1).trim();
        switchTotalData.setUptime(uptime.substring(0,uptime.length()-11).replace(" weeks,", "w").replace(" days", "d").replace(" hours", "h").replace(" minutes", "m"));
        switchTotalData.setVersion(s[22].substring(s[22].indexOf(":")+1).trim());
        return switchTotalData;
    }

    @Override
    public String getVlan(TelnetUtils telnetUtils, String command) {
        String result=telnetUtils.sendCommandVlan(command);
        return result;
    }

    @Override
    public void displayPage(TelnetUtils telnetUtils, String command) {
        telnetUtils.sendCommand(command);
    }

    @Override
    public String getIpv4Route(TelnetUtils telnetUtils, String command) {
        telnetUtils.setPrompt("*");
        String result=telnetUtils.sendCommand(command,2);
        telnetUtils.setPrompt("#");
        return result;
    }

    @Override
    public String getIpv6Route(TelnetUtils telnetUtils, String command) {
        telnetUtils.setPrompt("*");
        String result=telnetUtils.sendCommand(command,2);
        telnetUtils.setPrompt("#");
        return result;
    }

    @Override
    public String getConfiguration(TelnetUtils telnetUtils, String command) {
        telnetUtils.setPrompt("*");
        String result=telnetUtils.sendCommand(command);
        return result;
    }

    @Override
    public String getInterface(TelnetUtils telnetUtils, String command) {
        String result = telnetUtils.sendCommand(command,3);
        return result;
    }

    @Override
    public List<SwitchInterfaceStatus> explainInterfaceStatus(String interfaceMsg, String switchName, Map<String, String> statusMap) {
        List<SwitchInterfaceStatus> switchInterfaceStatusList = new ArrayList<>();
        SwitchInterfaceStatus switchInterfaceStatus;
        String[] temp = interfaceMsg.split("\n");
        int remark = 0;
        for (String s : temp) {
            if (remark != 0&&!s.contains("=")) {
                String portName=s.substring(0,5).trim();
                String status=s.substring(41,47).trim();
                String tempStatus="";
                if(status.equals("E")){
                    tempStatus="UP";
                }else{
                    tempStatus="DOWN";
                }
                switchInterfaceStatus = new SwitchInterfaceStatus();
                switchInterfaceStatus.setSwitchName(switchName);
                switchInterfaceStatus.setOriginalInterface(portName);
                switchInterfaceStatus.setSwitchInterface(portName);
                if (null!=statusMap&&null!=statusMap.get(status)&&!"".equals(statusMap.get(tempStatus))) {
                    switchInterfaceStatus.setSwitchStatus(Long.parseLong(statusMap.get(tempStatus)));
                } else {
                    switchInterfaceStatus.setSwitchStatus(-1);
                }
                switchInterfaceStatusList.add(switchInterfaceStatus);
            }
            if (s.contains("=")&&remark==0) {
                remark = 1;
            }else if(s.contains("=")&&remark==1){
                remark = 0;
            }
        }
        return switchInterfaceStatusList;
    }

    @Override
    public List<SwitchPortData> getInterfaceDetails(TelnetUtils telnetUtils, String command, String programDate, CabSwitch cabSwitch, Map<String, SwitchPortData> beforeSwitchPortDataMap) {
        List<SwitchPortData> switchPortDataList=new ArrayList<>();
        SwitchPortData switchPortData;
        String result=telnetUtils.sendCommand(command);
        String[] arg = result.split("\n");
        int remark = 0;
        for (String s:arg) {
            if (remark != 0&&!s.contains("=")) {
                String []port=s.replaceAll("\\s{1,}", " ").split(" ");
                switchPortData=new SwitchPortData();
                switchPortData.setSwitchName(cabSwitch.getName());
                switchPortData.setPortName(port[0]);
                switchPortData.setIp(cabSwitch.getAddress());
                if(beforeSwitchPortDataMap.containsKey(cabSwitch.getName()+":"+port[0])){
                    SwitchPortData before=beforeSwitchPortDataMap.get(cabSwitch.getName()+":"+port[0]);
                    switchPortData.setOutput((Double.parseDouble(port[3])-before.getOutputTotal())/15*60/1024/1024);
                    switchPortData.setInput((Double.parseDouble(port[5])-before.getInputTotal())/15*60/1024/1024);
                }else{
                    switchPortData.setInput(Double.parseDouble(port[5]));
                    switchPortData.setOutput(Double.parseDouble(port[3]));
                }
                switchPortData.setInputTotal(Double.parseDouble(port[5]));
                switchPortData.setOutputTotal(Double.parseDouble(port[3]));
                switchPortData.setProgramTime(programDate);
                switchPortData.setTxt(s);
                switchPortDataList.add(switchPortData);
            }
            if (s.contains("=")&&remark==0) {
                remark = 1;
            }else if(s.contains("=")&&remark==1){
                remark = 0;
            }
        }
        return switchPortDataList;
    }

    @Override
    public void exitEquipment(TelnetUtils telnetUtils, String command) {
        telnetUtils.sendCommand(command);
    }
}
